package com.sx.art2.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sx.art2.beans.Account;
import com.sx.art2.beans.Auction;
import com.sx.art2.beans.ShangjiaAccount;
import com.sx.art2.mapper.AccountMapper;
import com.sx.art2.mapper.ArtMapper;
import com.sx.art2.mapper.AuctionInfoMapper;
import com.sx.art2.mapper.AuctionMapper;
import com.sx.art2.mapper.ShangjiaAccountMapper;
import com.sx.art2.service.AuctionService;
import com.sx.art2.tools.Page;
@Service
public class AuctionServiceImpl implements AuctionService {

	@Autowired
	AuctionMapper auctionMapper;
	@Autowired
	ArtMapper artMapper;
	@Autowired
	AuctionInfoMapper auctionInfoMapper;
	@Autowired
	AccountMapper accountMApper;
	@Autowired
	ShangjiaAccountMapper shangjiaAccountMapper; 
	@Override
	@Transactional
	public void addAuction(Auction auction) {
		// TODO 自动生成的方法存根
        
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		//1.判断是否有上一位出价人出价，有则返还上一出价人的保证金
		ShangjiaAccount shangjiaAccount2=shangjiaAccountMapper.selectByAuctionInfoId(auction.getAuctionInfoId());
		if(shangjiaAccount2!=null)
		{
			Account account2=new Account();
			account2.setUserId(shangjiaAccount2.getUserId());
			account2.setAccountType("4");
			account2.setAccountTrade(shangjiaAccount2.getAccountTrade());
			account2.setAccountDate(df.format(new Date()));
			account2.setAccountBalance(accountMApper.selectBalanceByUserId(shangjiaAccount2.getUserId())+shangjiaAccount2.getAccountTrade());
			accountMApper.add(account2);
			
			
			shangjiaAccount2.setAccountBalance(shangjiaAccountMapper.selectBalance() -shangjiaAccount2.getAccountTrade());
			shangjiaAccount2.setAccountTrade(shangjiaAccount2.getAccountTrade());
			shangjiaAccount2.setAccountType(2);
			shangjiaAccount2.setAccountDate(df.format(new Date()));
			//shangjiaAccount2.setUserId(auction.getUserId());
			shangjiaAccount2.setObjectId(auction.getAuctionInfoId()+"");
			shangjiaAccountMapper.add(shangjiaAccount2);
			
			auctionMapper.updateTopPriceState(Integer.parseInt(shangjiaAccount2.getObjectId()));
		}
		//2.扣除出价人保证金，出价的30%		
		Account account=new Account();
		account.setUserId(auction.getUserId());
		account.setAccountType("3");
		account.setAccountTrade(auction.getAuctionPrice()*3/10);
		account.setAccountBalance(accountMApper.selectBalanceByUserId(auction.getUserId())-auction.getAuctionPrice()*3/10);			
		account.setAccountDate(df.format(new Date()));
		accountMApper.add(account);
		
		ShangjiaAccount shangjiaAccount=new ShangjiaAccount();
		shangjiaAccount.setAccountBalance(shangjiaAccountMapper.selectBalance()+account.getAccountTrade());
		shangjiaAccount.setAccountTrade(account.getAccountTrade());
		shangjiaAccount.setAccountType(1);
		shangjiaAccount.setAccountDate(df.format(new Date()));
		shangjiaAccount.setUserId(auction.getUserId());
		shangjiaAccount.setObjectId(auction.getAuctionInfoId()+"");
		shangjiaAccountMapper.add(shangjiaAccount);
		//3.插入一条出价记录
		auctionMapper.insert(auction);
		//更新拍卖次数
		auctionInfoMapper.updateAuctionNum(auction.getAuctionInfoId());
		//4.更新艺术品的当前价
		auctionInfoMapper.updateAuctionNowPrice(auction.getAuctionPrice(), auction.getAuctionInfoId());
			
		//5.判断出价时间，如果在结束时间的十分钟之内，则延时十分钟	
		auctionInfoMapper.auctionDefer(auction.getAuctionTime(), auction.getAuctionId());
	}
	@Override
	public List<Auction> selectByArtId(int artId) {
		// TODO 自动生成的方法存根
		return auctionMapper.selectByArtId(artId);
	}
	@Override
	public List<Auction> selectByUserId(int userId) {
		// TODO 自动生成的方法存根
		return auctionMapper.selectByUserId(userId);
	}
	@Override
	public List<Auction> selectByArtId(int artId, Page page) {
		// TODO 自动生成的方法存根
		return auctionMapper.pageSelectByArtId(artId, page);
	}
	@Override
	public List<Auction> selectByUserId(int userId, Page page) {
		// TODO Auto-generated method stub
		return auctionMapper.pageSelectByUserId(userId, page);
	}
	@Override
	public List<Auction> selectByauctionInfoId(int auctionInfoId) {
		// TODO Auto-generated method stub
		return auctionMapper.selectByauctionInfoId(auctionInfoId);
	}

}
